from wxpy import *
from socket import *
from time import ctime
from time import localtime
import time


class WeChatServer():
    def __init__(self):
        self.robot = Robot()
        self.friends = self.robot.friends()
        self.friend = None
        self.tuling = utils.Tuling("1103b16fba0d44c1be89d761029a532f")
        # 打印来自其他好友、群聊和公众号的消息
        @self.robot.register()
        def print_others(msg):
            print(msg)        
        # 回复 friend 的消息 (优先匹配后注册的函数!)
        @self.robot.register(self.friend)
        def reply_my_friend(msg):
            return '{} ({})'.format(msg.text, msg.type)            
    def send_text(self, msg, friendName='space', **attributes):
        self.friend = self.friends.search(friendName, **attributes)[0]
        self.friend.send(msg)
    def send_image(self, img, friendName="space", **attributes):
        self.friend = self.friends.search(friendName, **attributes)[0]
        self.friend.send_image(img)


    def start(self):
        # 开始监听和自动处理消息
        self.robot.start()        

if __name__ == '__main__':
    wechat = WeChatServer()
    wechat.send_text("hello", "space")
    wechat.start()
    